﻿using Hangfire;
using Hangfire.Logging;
//using Hangfire.RecurringJobExtensions;
using Hangfire.Server;
using Microsoft.AspNetCore.Mvc;
using NetCoreSample;
using Newtonsoft.Json;
using Serilog;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Linq;
using WebAppHangfire.Service;
//using BackgroundService = Hangfire.RecurringJobExtensions.BackgroundService;

namespace WebAppHangfire.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public partial class JobController : ControllerBase
    {

        private readonly ILogger<JobController> _logger;

        public JobController(ILogger<JobController> logger)
        {
            _logger = logger;
        }
        [HttpPost("httpjob")]
        public AddBackgroundHangfirJobResult Index(string op)
        {
            var res = new AddBackgroundHangfirJobResult { IsSuccess = true };
            var steam = new StreamReader(Request.Body);
            var job = steam.ReadToEndAsync().GetAwaiter().GetResult();

            var jobname = JsonConvert.DeserializeObject<BackgroundJobCom>(job);
            switch (op)
            {
                case "deljob":
                    try
                    {
                        //获取任务名称

                        Console.WriteLine(job);
                        RecurringJob.RemoveIfExists(jobname?.JobName);
                    }
                    catch (Exception ec)
                    {
                    }
                    //return new JsonResult(new { code = 0, msg = "ok", data = "", success = true });
                    res.ErrMessage = "删除成功";
                    break;
                case "recurringjob":

                    try
                    {

                        //var steam = new StreamReader(Request.Body);
                        //var job = steam.ReadToEndAsync().GetAwaiter().GetResult();

                        //var jobname = JsonConvert.DeserializeObject<BackgroundJobCom>(job);

                        if (string.IsNullOrWhiteSpace(jobname?.Cron))
                        {
                            jobname.Cron = "*/10 * * * * ?";
                        }

                        //RecurringJob.AddOrUpdate<MyJob1>(jobname.JobName, x => x.ExecuteTask, jobname.Cron);
                        RecurringJob.AddOrUpdate(jobname.JobName, () => Console.WriteLine("....k"), jobname.Cron, new RecurringJobOptions
                        {
                            TimeZone = TimeZoneInfo.Local
                        }); ;//每8秒

                        var jobId = BackgroundJob.Schedule(() => Console.WriteLine("Delayed 10!"), TimeSpan.FromSeconds(jobname.DelayFromMinutes));
                        BackgroundJob.ContinueJobWith(jobId, () => Console.WriteLine("Delayed--Continuation!"));
                    }
                    catch (Exception ec)
                    {
                        res.ErrMessage = ec.Message;
                        res.IsSuccess = false;
                    }

                    break;
                case "backgroundjob":

                    //var jobname = JsonConvert.DeserializeObject<BackgroundJobCom>(job);

                    if (string.IsNullOrWhiteSpace(jobname?.Cron))
                    {
                        jobname.Cron = "*/10 * * * * ?";
                    }

                    res.ErrMessage = "";
                    res.JobId = "AddJobController-id";
                    break;
                default:
                    break;
            }
            return res;
        }

    }

}
